//
//  BiDocInfoUploadViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15/5/6.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiDocInfoUploadViewController.h"

#import "BiCarLiscenceComEntity.h"
#import "BiCarLicense.h"
#import "BICarLicenseType.h"

#import "BiDatePickerView.h"

@interface BiDocInfoUploadViewController ()<UITableViewDelegate,BiDatePickerViewDelegate>{
    
    __weak IBOutlet UIScrollView *_ScrollView;
    
    BiDatePickerView * _datePickerView;
    
    NSInteger carType;
    
    int dateType;  //1为登记时间，2为有效时间
}
@property (weak, nonatomic) IBOutlet UITextView *textVReasons;

@property (weak, nonatomic) IBOutlet UILabel *labVin;
@property (weak, nonatomic) IBOutlet UILabel *labMenberName;
@property (weak, nonatomic) IBOutlet UILabel *labBusinessJiesuanMoney;
@property (weak, nonatomic) IBOutlet UILabel *labNoJiesuanMoney;
@property (weak, nonatomic) IBOutlet UILabel *labSaleAdvisorName;
@property (weak, nonatomic) IBOutlet UILabel *labRegisteTime;
@property (weak, nonatomic) IBOutlet UILabel *labValidTime;
@property (weak, nonatomic) IBOutlet UILabel *labLicensePlate;

@property (weak, nonatomic) IBOutlet UILabel *labEarnMemberPrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnCostPrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnDetectionPrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnManagementPrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnServicePrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnAgencyPrice;
@property (weak, nonatomic) IBOutlet UILabel *labEarnTotalPrice;

@property (weak, nonatomic) IBOutlet UILabel *labInfoPifuPersonName;
@property (weak, nonatomic) IBOutlet UILabel *labInfoPifuTime;


@property (weak, nonatomic) IBOutlet UIButton *btnRegisterTime;
@property (weak, nonatomic) IBOutlet UIButton *btnValidityTime;
@property (weak, nonatomic) IBOutlet UIButton *btnLicenseNum;
@end

@implementation BiDocInfoUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBackBtn];
    [self setNavBarTitle:@"信息上传"];
    [self setNavBarWithHidden:NO];
    
    _ScrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, 965);
    self.textVReasons.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.textVReasons.layer.borderWidth = 0.5;
    
    [self dataFill];
    [self lableLayerSetting];
    
}

- (void)lableLayerSetting{
    self.btnRegisterTime.layer.borderWidth = 0.5;
    self.btnRegisterTime.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.btnRegisterTime.layer.cornerRadius = 5;
    
    self.btnValidityTime.layer.borderWidth = 0.5;
    self.btnValidityTime.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.btnValidityTime.layer.cornerRadius = 5;
    
    self.btnLicenseNum.layer.borderWidth = 0.5;
    self.btnLicenseNum.layer.borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:100.0].CGColor;
    self.btnLicenseNum.layer.cornerRadius = 5;
}

- (void)dataFill{
    _textVReasons.text = self.cEntity.Carlisce.infoRejectReason;
    
    _labVin.text = self.cEntity.Carlisce.vin;
    _labMenberName.text = self.cEntity.Carlisce.memberName;
    _labBusinessJiesuanMoney.text = self.cEntity.Carlisce.businessJiesuanMoney;
    _labNoJiesuanMoney.text = self.cEntity.Carlisce.noJiesuanMoney;
    _labSaleAdvisorName.text = self.cEntity.Carlisce.saleAdvisorName;
    _labRegisteTime.text = self.cEntity.CarlisceType.registeTime;
    _labValidTime.text = self.cEntity.CarlisceType.validTime;
    _labLicensePlate.text = self.cEntity.CarlisceType.licensePlate;
    
    _labEarnMemberPrice.text = self.cEntity.CarlisceType.earnMemberPrice;
    _labEarnCostPrice.text = self.cEntity.CarlisceType.earnCostPrice;
    _labEarnDetectionPrice.text = self.cEntity.CarlisceType.earnDetectionPrice;
    _labEarnManagementPrice.text = self.cEntity.CarlisceType.earnManagementPrice;
    _labEarnServicePrice.text = self.cEntity.CarlisceType.earnServicePrice;
    _labEarnAgencyPrice.text = self.cEntity.CarlisceType.earnAgencyPrice;
    _labEarnTotalPrice.text = self.cEntity.CarlisceType.earnTotalPrice;
    
    _labInfoPifuPersonName.text = self.cEntity.Carlisce.infoPifuPersonName;
    _labInfoPifuTime.text = self.cEntity.Carlisce.infoPifuTime;
}

//保存
- (IBAction)btnCheckClick:(UIButton *)sender {
    
//    if (0 != carType && [BiUtil isBlankString:_textfLicensePlate.text]) {
//        
//        [self showMessage:@"车牌号未填写"];
//        
//        return;
//    }

    NSString * registeStr = _labRegisteTime.text;
    if ([BiUtil isBlankString:registeStr]) {
        
        [self showMessage:@"出发时间未选择"];
        
        return;
    }
    NSString * validStr = _labValidTime.text;
    if ([BiUtil isBlankString:validStr]) {
        
        [self showMessage:@"返回时间未选择"];
        
        return;
    }
    
    
    NSDate * registeDate =  [BiUtil dateFromFomate:registeStr formate:@"yyyy-MM-dd hh:mm"];
    NSDate * validDate =  [BiUtil dateFromFomate:validStr formate:@"yyyy-MM-dd hh:mm"];
    if (![validDate laterDate:registeDate]) {
        
        [self showMessage:@"亲，返回时间早于出发时间"];
        
        return;
    }
    
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.cEntity.CarlisceType.licenseTypeId,@"licenseTypeId",
                   self.cEntity.CarlisceType.registeTime,@"registeTime",
                   self.cEntity.CarlisceType.validTime,@"validTime",
                   self.cEntity.CarlisceType.licensePlate,@"licensePlate",
                   nil];
        
        
    
    [[BiNetEngine shareInstance] requestDocInfoSave:self paraDict:paraDic];
    
    [self asyshowHUDView:WAITING CurrentView:self];
    
}

//提交
- (IBAction)btnUploadClick:(UIButton *)sender {
    
    //    if (0 != carType && [BiUtil isBlankString:_textfLicensePlate.text]) {
    //
    //        [self showMessage:@"车牌号未填写"];
    //
    //        return;
    //    }
    
    NSString * registeStr = _labRegisteTime.text;
    if ([BiUtil isBlankString:registeStr]) {
        
        [self showMessage:@"出发时间未选择"];
        
        return;
    }
    NSString * validStr = _labValidTime.text;
    if ([BiUtil isBlankString:validStr]) {
        
        [self showMessage:@"返回时间未选择"];
        
        return;
    }
    
    
    NSDate * registeDate =  [BiUtil dateFromFomate:registeStr formate:@"yyyy-MM-dd hh:mm"];
    NSDate * validDate =  [BiUtil dateFromFomate:validStr formate:@"yyyy-MM-dd hh:mm"];
    if (![validDate laterDate:registeDate]) {
        
        [self showMessage:@"亲，返回时间早于出发时间"];
        
        return;
    }
    
    NSDictionary * paraDic = @{@"id":self.cEntity.Carlisce.ID,
                               @"licenseStatus":@"4",
                               @"status":@"1"};
//    NSDictionary * paraDic = nil;
    
    [[BiNetEngine shareInstance] requestDocInfoUploadCheck:self paraDict:paraDic];
    
    [self asyshowHUDView:WAITING CurrentView:self];
    
}
#pragma mark BiDatePickerViewDelegate
- (void)onDatePickerViewDateSelect:(id)sender date:(NSDate*)date
{
    
    [_datePickerView removeFromSuperview];
    
    NSString * dataeStr = [BiUtil stringFromFomate:date formate:@"yyyy-MM-dd hh:mm"];
    
    if (1 == dateType) {
        _labRegisteTime.text = dataeStr;
        
    }else if (2 == dateType){
        
        _labValidTime.text = dataeStr;
    }
    
    if (dateType == 2 && isIPhone4) {
        
        _ScrollView.frame = CGRectMake(0, _ScrollView.frame.origin.y + 100, MAIN_SCREEN_WIDTH, _ScrollView.frame.size.height);
    }
    
}

- (IBAction)btnInputClick:(UIButton *)sender {
    
//    if (10 == sender.tag) {
//        
//        if (_carTypeTable) {
//            
//            
//            [self.view.window addSubview:_cartypeBgView];
//        }
//        
//    }else{
    
        dateType = (int)sender.tag - 10;
        
        if (dateType == 2 && isIPhone4) {
            
            _ScrollView.frame = CGRectMake(0, _ScrollView.frame.origin.y - 100, MAIN_SCREEN_WIDTH, _ScrollView.frame.size.height);
        }
        
        if (!_datePickerView) {
            
            _datePickerView = [[BiDatePickerView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, self.view.frame.size.height)];
            
            [_datePickerView setDateModeandButton];
            
            _datePickerView.delegate = self;
        }
        
        [self.view addSubview:_datePickerView];
        
//    }
}

#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
        return;
        
    }
    
    switch (tag) {
        case NET_GET_CAR_DOC_CHECK:
        {
            [self.navigationController popViewControllerAnimated:YES];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
    
}

@end
